package cn.iocoder.yudao.module.pay.controller.admin.collect;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pay.controller.admin.collect.vo.PayCollectInfoPageReqVO;
import cn.iocoder.yudao.module.pay.controller.admin.collect.vo.PayCollectInfoRespVO;
import cn.iocoder.yudao.module.pay.controller.admin.collect.vo.PayCollectInfoSaveReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.paycollectinfo.PayCollectInfoDO;
import cn.iocoder.yudao.module.pay.service.paycollectinfo.PayCollectInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 收款账户信息")
@RestController
@RequestMapping("/pay/collect-info")
@Validated
public class PayCollectInfoController {

    @Resource
    private PayCollectInfoService collectInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建收款账户信息")
    @PreAuthorize("@ss.hasPermission('pay:collect-info:create')")
    public CommonResult<Long> createCollectInfo(@Valid @RequestBody PayCollectInfoSaveReqVO createReqVO) {
        return success(collectInfoService.createCollectInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新收款账户信息")
    @PreAuthorize("@ss.hasPermission('pay:collect-info:update')")
    public CommonResult<Boolean> updateCollectInfo(@Valid @RequestBody PayCollectInfoSaveReqVO updateReqVO) {
        collectInfoService.updateCollectInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除收款账户信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pay:collect-info:delete')")
    public CommonResult<Boolean> deleteCollectInfo(@RequestParam("id") Long id) {
        collectInfoService.deleteCollectInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得收款账户信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pay:collect-info:query')")
    public CommonResult<PayCollectInfoRespVO> getCollectInfo(@RequestParam("id") Long id) {
        PayCollectInfoDO collectInfo = collectInfoService.getCollectInfo(id);
        return success(BeanUtils.toBean(collectInfo, PayCollectInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得收款账户信息分页")
    @PreAuthorize("@ss.hasPermission('pay:collect-info:query')")
    public CommonResult<PageResult<PayCollectInfoRespVO>> getCollectInfoPage(@Valid PayCollectInfoPageReqVO pageReqVO) {
        PageResult<PayCollectInfoDO> pageResult = collectInfoService.getCollectInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PayCollectInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出收款账户信息 Excel")
    @PreAuthorize("@ss.hasPermission('pay:collect-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCollectInfoExcel(@Valid PayCollectInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PayCollectInfoDO> list = collectInfoService.getCollectInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "收款账户信息.xls", "数据", PayCollectInfoRespVO.class,
                        BeanUtils.toBean(list, PayCollectInfoRespVO.class));
    }

}